package br.ufal.ic.manager;

import br.ufal.ic.core.Ranking;
import br.ufal.ic.core.Tournament;
import br.ufal.ic.dao.FacadeDAO;
import br.ufal.ic.dao.RankingDAO;
import java.util.Map;

/**
 * Gerenciador de rankings.
 *
 * @author Marcos Paulo
 */
public class RankingManager {

    private static RankingManager instance = new RankingManager();
    /**
     * ranking geral, envolve dados de outros torneios.
     */
    private Ranking generalRanking;
    /**
     * relaciona um torneio a um ranking.
     */
    private Map<Tournament, Ranking> tournamentRankings;

    private RankingManager() {
        initGeneralRanking();
//        tournamentRankings = new HashMap<Tournament, Ranking>();
    }

    private void initGeneralRanking() {
        String rankingId = "general-ranking";

        RankingDAO rankingDAO = FacadeDAO.getInstance().getRankingDAO();
        generalRanking = rankingDAO.findById(rankingId);

        if (generalRanking == null) {
            generalRanking = new Ranking("ranking de proposito geral.");

            generalRanking.setId(rankingId);
        }

    }

    public static RankingManager getInstance() {
        return instance;
    }

    public Ranking getGeneralRanking() {
        return generalRanking;
    }

//    @Override
//    protected void finalize() throws Throwable {
//        super.finalize();
//        RankingDAO rankingDAO = FacadeDAO.getInstance().getRankingDAO();
//
//        rankingDAO.update(generalRanking);
//
//        for(Ranking ranking: tournamentRankings.values()) {
//            rankingDAO.update(ranking);
//        }
//    }

}
